package cn.flying.cloud.task.controller;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.task.bo.TaskJobInfoBo;
import cn.flying.cloud.task.service.TaskJobInfoService;

/**
 * 定时任务配置信息 前端控制器
 *
 * @author: admin
 * @date: 2025-09-24 10:53:52
 * @version: 1.0
 */
@RestController
@RequestMapping("/task/jobInfo")
public class TaskJobInfoController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private TaskJobInfoService taskJobInfoService;

    /**
     * 分页查询定时任务配置信息数据
     *
     * @param request 参数
     * @return
     */
    @PostMapping("page")
    public PageResponse<TaskJobInfoBo> page(@RequestBody PageRequest<TaskJobInfoBo> request) {
        return taskJobInfoService.getPageList(request);
    }

    /**
     * 列表查询定时任务配置信息数据
     *
     * @param param 参数
     * @return
     */
    @PostMapping("list")
    public Rt<?> list(@RequestBody TaskJobInfoBo param) {
        return Rt.success(taskJobInfoService.getList(param));
    }

    /**
     * 查询定时任务配置信息详情数据
     *
     * @param param 参数
     * @return
     */
    @PostMapping("detail")
    public Rt<?> detail(TaskJobInfoBo param) {
        return Rt.success(taskJobInfoService.getOneObj(param));
    }

    /**
     * 根据主键ID查询定时任务配置信息详情数据
     *
     * @param id 主键ID
     * @return
     */
    @PostMapping("getById")
    public Rt<?> getById(Long id) {
        return Rt.success(taskJobInfoService.getObjById(id));
    }

    /**
     * 保存定时任务配置信息数据
     *
     * @param param 参数
     * @return
     */
    @PostMapping("save")
    public Rt<?> save(@RequestBody TaskJobInfoBo param) {
        if (Objects.isNull(param.getId())) {
            // 新增
            taskJobInfoService.createObj(param);
            return Rt.success("新增任务成功！");
        } else {
            // 更新
            taskJobInfoService.updateObj(param);
            return Rt.success("所属【" + param.getJobGroup() + "】任务【" + param.getJobName() + "】修改成功！");
        }
    }

    /**
     * 删除定时任务配置信息数据
     *
     * @param param 参数
     * @return
     */
    @PostMapping("delete")
    public Rt<?> delete(TaskJobInfoBo param) {
        taskJobInfoService.deleteObj(param);
        return Rt.success("所属【" + param.getJobGroup() + "】任务【" + param.getJobName() + "】删除成功！");
    }

    /**
     * 根据主键ID删除定时任务配置信息数据
     *
     * @param id 主键ID
     * @return
     */
    @GetMapping("deleteById/{id}")
    public Rt<?> deleteById(@PathVariable("id") Long id) {
        taskJobInfoService.deleteById(id);
        return Rt.success("删除成功！");
    }

    /**
     * 启动任务
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("start")
    public Rt<?> startJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup) {
        taskJobInfoService.startJob(jobName, jobGroup);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】启动成功！");
    }


    /**
     * 暂停任务
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("pause")
    public Rt<?> pauseJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup) {
        taskJobInfoService.pauseJob(jobName, jobGroup);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】暂停运行！");
    }

    /**
     * 恢复任务
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("resume")
    public Rt<?> resumeJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup) {
        taskJobInfoService.resumeJob(jobName, jobGroup);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】恢复运行！");
    }

    /**
     * 停止任务（表示完全不用了，下线了，如需重新启用，需要使用如下方法重新调度：）
     * // 重新添加作业和触发器
     * scheduler.scheduleJob(job, trigger);
     * // 重新启动调度器
     * scheduler.start();
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("stop")
    public Rt<?> stopJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup) {
        taskJobInfoService.stopJob(jobName, jobGroup);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】停止运行！");
    }

    /**
     * 立即触发执行一次任务
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("trigger")
    public Rt<?> triggerJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup, String param) {
        taskJobInfoService.triggerJob(jobName, jobGroup, param);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】运行成功！");
    }

    /**
     * 手动补偿处理
     *
     * @param jobName
     * @param jobGroup
     * @return
     */
    @RequestMapping("resolve")
    public Rt<?> resolveJob(@NotBlank(message = "任务名称不能为空") String jobName, @NotBlank(message = "所属分组不能为空") String jobGroup, String param, @NotBlank(message = "执行节点不能为空") String host, @NotBlank(message = "补偿单号不能为空") String serialNo) {
        taskJobInfoService.resolveJob(jobName, jobGroup, serialNo, param, host);
        return Rt.success("所属【" + jobGroup + "】任务【" + jobName + "】运行成功！");
    }

}
